package com.hlwxy.ssm.cxf;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import javax.xml.namespace.QName;


public class Test {
    public static void main(String[] args) throws Exception {

     JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(StudentService.class);
        factory.setAddress("http://localhost:9080/webservice/student?wsdl");
        StudentService service = (StudentService) factory.create();
        service.sayHello();



     //动态调用
       JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:9080/webservice/student?wsdl");
        //sayHello 为接口中定义的方法名称   张三为传递的参数   返回一个Object数组
        QName opName = new QName("http://cxf.ssm.hlwxy.com/", "sayHello2");  // 指定到接口类所在包
        Object[] objects=client.invoke(opName, "张三");
        System.out.println(objects[0].toString());

    }

    /**
     * 动态调用
     * @param url
     * @param method
     * @param parameters
     * @return
     */
    public static Object[] invokeRemoteMethod(String url, String method, Object[] parameters) {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        if (!url.endsWith("wsdl")) {
            url += "?wsdl";
        }
        org.apache.cxf.endpoint.Client client = dcf.createClient(url);
        try {
            Object[] objects = client.invoke(method, parameters);
            return objects;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
